package dev.projectnightfall.network;

import dev.projectnightfall.main.ConsoleLog;
import dev.projectnightfall.network.packets.APacket;
import dev.projectnightfall.network.packets.Packet1StayAlive;
import dev.projectnightfall.network.packets.Packet2Login;
import java.util.ArrayList;
import java.util.logging.Level;

/**
 * Manager class of all the various packets.
 */
public class PacketManager {

    private ArrayList<APacket> packetsList = null;

    /**
     * Constructor which registers necessary packets.
     */
    public PacketManager() {
        packetsList = new ArrayList<APacket>();

        registerPacket(new Packet1StayAlive());
        registerPacket(new Packet2Login());
    }

    /**
     * Returns the packet associated with this ID.
     *
     * @param id Unique ID of the packet to return.
     * @return Packet with the unique ID. If none are found, returns null.
     */
    public APacket getPacket(int id) {
        APacket[] packetsArr = (APacket[]) packetsList.toArray();
        for (int i = 0; i < packetsArr.length; i++) {
            if (packetsArr[i].getID() == id) {
                return packetsArr[i];
            }
        }
        return null;
    }

    /**
     * Executes the given String according to the packet. Might throw Exception
     * at a later date.
     *
     * @param p Data received and to be split and executed.
     */
    public void executePacket(String p) {
        String[] packetArr = p.split(":");
        //packetID:packetData
        if (p.length() == 2) {
            int packetID = 0;
            boolean success = false;
            String packetData = "";

            try {
                packetData = packetArr[1];
                packetID = Integer.parseInt(packetArr[0]);
            } catch (Exception ex) {
                success = false;
                ConsoleLog.print("Packet ID not valid. " + packetArr[0], Level.SEVERE);
            }
            if (success) {
                //seriously need to refine this
                APacket[] packetsArr = (APacket[]) packetsList.toArray();
                for (int i = 0; i < packetsArr.length; i++) {
                    if (packetsArr[i].getID() == packetID) {
                        packetsArr[i].execute(p);
                    }
                }
            }
        } else {
            ConsoleLog.print("Invalid packet received. " + p, Level.SEVERE);
        }
    }

    /**
     * Checks if the packet is already registered on the system.
     *
     * @param p Packet to compare with.
     * @return Returns true or false depending if found or not.
     */
    public boolean checkPacketRegistered(APacket p) {
        APacket[] packetsArr = (APacket[]) packetsList.toArray();
        for (int i = 0; i < packetsArr.length; i++) {
            if (packetsArr[i].getID() == p.getID()) {
                return true;
            }
        }
        return false;
    }

    /**
     * Registers the packet to be used in this session.
     *
     * @param p Packet to be registered on the list.
     */
    public void registerPacket(APacket p) {
        if (!checkPacketRegistered(p)) {
            packetsList.add(p);
        }
    }
}
